//
//  DoctorReserveTopView.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2020/2/26.
//  Copyright © 2020 tao Tao. All rights reserved.
//

import UIKit

protocol DoctorReserveTopViewDelegate: NSObjectProtocol {
    func didClickedDoctorReserveTopView(type: DoctorReserveTopViewType, dismiss: Bool)
}

enum DoctorReserveTopViewType: Int {
    case department
    case doctorType
    case office
    case prescription
}

class DoctorReserveTopView: UIView {
    
    weak var delegate: DoctorReserveTopViewDelegate?
    
    @IBOutlet private var btnArray: [UIButton]!

    @IBOutlet private weak var departmentBtn: UIButton! {
        didSet {
            departmentBtn.layoutButton(with: .imageRight)
            departmentBtn.setTitleColor(.main, for: .selected)
            departmentBtn.setBackgroundImage(UIImage(color: .white), for: .normal)
            departmentBtn.setBackgroundImage(UIImage(color: .mainLight), for: .highlighted)
        }
    }
    
    @IBOutlet private weak var doctorOfficeBtn: UIButton! {
        didSet {
            doctorOfficeBtn.layoutButton(with: .imageRight)
            doctorOfficeBtn.setTitleColor(.main, for: .selected)
            doctorOfficeBtn.setBackgroundImage(UIImage(color: .white), for: .normal)
            doctorOfficeBtn.setBackgroundImage(UIImage(color: .mainLight), for: .highlighted)
        }
    }
    
    @IBOutlet private weak var prescriptionBtn: UIButton! {
        didSet {
            prescriptionBtn.layoutButton(with: .imageRight)
            prescriptionBtn.setTitleColor(.main, for: .selected)
            prescriptionBtn.setBackgroundImage(UIImage(color: .white), for: .normal)
            prescriptionBtn.setBackgroundImage(UIImage(color: .mainLight), for: .highlighted)
        }
    }
    
    @IBOutlet private weak var doctorTypeBtn: UIButton! {
        didSet {
            doctorTypeBtn.layoutButton(with: .imageRight)
            doctorTypeBtn.setTitleColor(.main, for: .selected)
            doctorTypeBtn.setBackgroundImage(UIImage(color: .white), for: .normal)
            doctorTypeBtn.setBackgroundImage(UIImage(color: .mainLight), for: .highlighted)
        }
    }
    
    @IBAction private func buttonClickAction(_ sender: UIButton) {
        btnArray.forEach {
            if $0.isSelected && $0 != sender {
                $0.isSelected.toggle()
                let index = btnArray.firstIndex(of: $0)!
                let type = DoctorReserveTopViewType(rawValue: index)!
                delegate?.didClickedDoctorReserveTopView(type: type, dismiss: !sender.isSelected)
            }
        }
        sender.isSelected.toggle()
        let index = btnArray.firstIndex(of: sender)!
        let type = DoctorReserveTopViewType(rawValue: index)!
        delegate?.didClickedDoctorReserveTopView(type: type, dismiss: !sender.isSelected)
    }
    
    func configure(with doctorType: DoctorType) {
        doctorTypeBtn.isHidden = doctorType != .all
    }
    
    func setTitle(_ title: String?, type: DoctorReserveTopViewType, isClicked: Bool = true) {
        if isClicked {
            let btn = btnArray[type.rawValue]
            btn.isSelected.toggle()
        }
        switch type {
        case .department:
            departmentBtn.relayout(with: title == "全部" ? "科室" : title, style: .imageRight)
        case .doctorType:
            doctorTypeBtn.relayout(with: title == "全部" ? "服务类型" : title, style: .imageRight)
        case .office:
            doctorOfficeBtn.relayout(with: title == "全部" ? "职称" : title, style: .imageRight)
        case .prescription:
            prescriptionBtn.relayout(with: title == "全部" ? "处方权限" : title, style: .imageRight)
        }
    }
    
    func reset(_ type: DoctorReserveTopViewType) {
        let button = btnArray[type.rawValue]
        button.isSelected.toggle()
    }
}
